<?php

namespace App\Store\Controllers\Sysset;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Sysset\Classesset;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Validate\Sysset\Classesset as ValidateBasicClassesset;

/**
 * 班次信息
 */
class ClassessetController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = Classesset::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = Classesset::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids','classes_name','remark']);

        $Validate = new ValidateBasicClassesset();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);

        
        $store_ids = $req_data['store_ids'];
        $store_ids = is_array($store_ids) ? $store_ids : (is_array(json_decode($store_ids, true)) ? json_decode($store_ids, true) : []);


        if (count($store_ids) <= 0) {

            return response()->json(['code' => '001', 'msg' => '无效门店参数']);
        }
        foreach($store_ids as $store_id){
            $storeinfo = SiteStore::where(['id' => $store_id, 'cp_id' => $accountinfo['cp_id']])->first();
        if (!$storeinfo) {
            return response()->json(['code' => '001', 'msg' => '无效门店参数']);
        }
            $isexist=Classesset::where([['store_id', $store_id], ['classes_name',$req_data['classes_name']]])->first();
            if($isexist){
                return response()->json(['code' => '001', 'msg' => '班次不允许重复']);
            }

            $ins_data[]=[
                'cp_id'=> $accountinfo['cp_id'],
                'store_id'=> $store_id,
                'classes_name'=>$req_data['classes_name'],
                'create_at'=>time(),
                'update_at'=>time(),
                'create_account_id' => $accountinfo['id'],
                'update_account_id' => $accountinfo['id'],
                'create_account_real_name' => $accountinfo['real_name'],
                'update_account_real_name' => $accountinfo['real_name'],
            ];

        }
        Classesset::insert($ins_data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'classes_name','remark']);

        $Validate = new ValidateBasicClassesset('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        
        $info = Classesset::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $isexist = Classesset::where([['id', '<>', $req_data['id']], ['classes_name', $req_data['classes_name']], ['cp_id', $accountinfo['cp_id']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '班次不允许重复']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id'] = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        Classesset::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = Classesset::where([['id', $id], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        Classesset::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
